package com.example.myuniversity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.university.dialog.GetDataDialog;
import com.university.dialog.GetHotUniversityDialog;
import com.university.dialog.GetMajorDataDialog;


public class MainActivity extends Activity {
	private ImageView imageSelect,imagePk,imageNews,imageHot;
	private LinearLayout layoutSelect,layoutPk,layoutNews,layoutHot;
	private Animation animation;
	private long exitTime;
    @SuppressLint("NewApi") @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置4.4以上版本状态栏颜色变化
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT&&Build.VERSION.SDK_INT<Build.VERSION_CODES.KITKAT_WATCH) {
		 	getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.color_back);
        } else 
	    	if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
	    	 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  
	    	 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN  
	                          | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION  
	                          | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);  
	          getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  
	          getWindow().setStatusBarColor(getResources().getColor(R.color.color_back));  
		}
        init();
        SharedPreferences preferences=this.getApplicationContext().getSharedPreferences("flag", MODE_PRIVATE);
        boolean flag= preferences.getBoolean("is_first", true);
        if (flag) {
        	new GetMajorDataDialog(this);
        	new GetDataDialog(this);
        	new GetHotUniversityDialog(this );
        	Intent intent=new Intent(this,GuideActivity.class);
			startActivity(intent);
			preferences.edit().putBoolean("is_first", false).commit();
		}else {
			Intent intent=new Intent(this,WelcomeActivity.class);
			startActivity(intent);
		}
        
        layoutSelect.setOnClickListener(new OnLayoutClick());
        layoutPk.setOnClickListener(new OnLayoutClick());
        layoutNews.setOnClickListener(new OnLayoutClick());
        layoutHot.setOnClickListener(new OnLayoutClick());
        
    }
    
    private void init(){
    	layoutSelect=(LinearLayout) findViewById(R.id.layout_select);
    	layoutPk=(LinearLayout) findViewById(R.id.layout_pk);
    	layoutNews=(LinearLayout) findViewById(R.id.layout_news);
    	layoutHot=(LinearLayout) findViewById(R.id.layout_hot);
    	imageSelect=(ImageView) findViewById(R.id.imageMainSelect);
    	imagePk=(ImageView) findViewById(R.id.image_main_pk);
    	imageNews=(ImageView) findViewById(R.id.image_main_news);
    	imageHot=(ImageView) findViewById(R.id.image_main_hot);
    	
    	LinearInterpolator lin=new LinearInterpolator();

    	animation=new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		animation.setDuration(3000);
		animation.setRepeatCount(Animation.INFINITE);
		animation.setInterpolator(lin);
    	imageSelect.startAnimation(animation);
    	
    	animation=new RotateAnimation(60, 419, Animation.RELATIVE_TO_SELF, 0.5f,
    			Animation.RELATIVE_TO_SELF, 0.5f);
    	animation.setDuration(3000);
    	animation.setRepeatCount(Animation.INFINITE);
    	animation.setInterpolator(lin);
    	imagePk.startAnimation(animation);
    	
    	animation=new RotateAnimation(120, 479, Animation.RELATIVE_TO_SELF, 0.5f,
    			Animation.RELATIVE_TO_SELF, 0.5f);
    	animation.setDuration(3000);
    	animation.setRepeatCount(Animation.INFINITE);
    	animation.setInterpolator(lin);
    	imageNews.startAnimation(animation);
    	
    	animation=new RotateAnimation(180, 539, Animation.RELATIVE_TO_SELF, 0.5f,
    			Animation.RELATIVE_TO_SELF, 0.5f);
    	animation.setDuration(3000);
    	animation.setRepeatCount(Animation.INFINITE);
    	animation.setInterpolator(lin);
    	imageHot.startAnimation(animation);
    }
    
    class OnLayoutClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			Intent intent;
			switch (v.getId()) {
			case R.id.layout_select:
				intent=new Intent(MainActivity.this,SelectActivity.class);
				startActivity(intent);
				break;
			case R.id.layout_pk:
				intent=new Intent(MainActivity.this,MajorPkActivity.class);
				startActivity(intent);
				break;
			case R.id.layout_news:
				break;
			case R.id.layout_hot:
				intent=new Intent(MainActivity.this,HotUniversityActivity.class);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
    }
    
    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if (keyCode == KeyEvent.KEYCODE_BACK) {
	            exit();
	            return false;
	        }
	        return super.onKeyDown(keyCode, event);
	    }

	    public void exit() {
	        if ((System.currentTimeMillis() - exitTime) > 3000) {
	            Toast.makeText(getApplicationContext(), "再次单击退出程序",
	                    Toast.LENGTH_SHORT).show();
	            exitTime = System.currentTimeMillis();
	        } else {
	            finish();
	            System.exit(0);
	        }
	    }
    
}
